package com.sky.service.impl;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        //写setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.add(setmeal);
        //写setmeal和dish的中间表 setmeal_dish
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.add(setmealDish);
        }
    }

    @Override
    public PageResult getPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        //todo 参考菜品进行优化
        List<Setmeal> list = setmealMapper.getList();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(list.size());
        pageResult.setRecords(list);
        return pageResult;
    }

    @Override
    public SetmealVO getById(Long setmealId) {
        SetmealVO setmealVO = new SetmealVO();
        //读套餐主表 setmeal
        Setmeal setmeal = setmealMapper.getById(setmealId);
        BeanUtils.copyProperties(setmeal, setmealVO);
        //读套餐_菜品关系表 setmeal_dish
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(setmealId);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public List<Setmeal> getListBycategoryId(Long categoryId) {
        SetmealVO setmealVO = new SetmealVO();
        //读套餐主表 setmeal
        List<Setmeal> list = setmealMapper.getListBycategoryId(categoryId);

        return list;
    }
}
